package com.du.springmvc.controller;

import com.du.springmvc.pojo.Father;
import com.du.springmvc.pojo.Person;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Locale;
import java.util.Map;

/**
 * @author : DuJiabao
 * @Date : 2021/3/29 23:23
 * @Project : spring-mvc
 * @File : null.java
 * @Desc :
 */
@Controller
public class RequestParamController {
  @RequestMapping("/requestParamTest")
  String requestParamTest(@RequestParam(value = "id", defaultValue = "0") Integer id,
                          @RequestParam(value = "name", required = true) String name) {
    System.out.println("id : " + id);
    System.out.println("name : " + name);
    System.out.println("Person : " + new Person(name, id));
    return "hello";
  }

  @RequestMapping("requestParamTest2")
  String requestParamTest2(Person person) {
    System.out.println("Person : " + person);
    return "hello";
  }

  @RequestMapping("/requestParamTest3")
  String requestParamTest3(Integer id, String name) {
    System.out.println("id : " + id);
    System.out.println("name : " + name);
    System.out.println("Person : " + new Person(name, id));
    return "hello";
  }

  @RequestMapping("/requestParamTest4")
  String requestParamTest4(Father father) {
    System.out.println("father : " + father);
    return "hello";
  }

  @RequestMapping("/requestHeaderTest")
  String requestHeaderTest(@RequestHeader("Accept-Language") String acceptLanguage,
                           @RequestHeader("User-Agent") String userAgent) {
    System.out.println("Accept-Language : " + acceptLanguage);
    System.out.println("User-Agent : " + userAgent);
    return "hello";
  }

  @RequestMapping("/cookieValueTest")
  String cookieValueTest(@CookieValue(value = "JSESSIONID", required = false) String JSESSIONID) {
    System.out.println("JSESSIONID : " + JSESSIONID);
    return "hello";
  }

  @RequestMapping("/rawApiTest")
  String rawApiTest(Locale locale) {
    System.out.println(locale);
    return "hello";
  }

  @ModelAttribute
  void getPerson(Map<String, Object> map) {
    map.put("person", new Person("name", 1));
  }

  @RequestMapping("multiParamTest")
  String multiParamTest(@RequestParam("value1") String value1, String value2, Person person, HttpServletRequest req, Map<String, Object> map) {
    System.out.println("value1 : " + value1);
    System.out.println("value2 : " + value2);
    System.out.println("person : " + person);
    System.out.println("req : " + req);
    System.out.println("map : " + map);
    map.put("value3", 3);
    return "hello";
  }
}
